/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.wanhai.itm.wditm144.vm;

import com.wanhai.itm.wditm141.vm.*;
import com.wanhai.itm.wditm141.service.WDITM141_Service;
import com.wanhai.vo.itm.ITM4005VO;
import com.wanhai.vo.itm.ITM4007VO;
import com.wanhai.vo.itm.ITM4008VO;
import com.wanhai.vo.itm.ITM4010VO;
import com.wanhai.zkbase.core.WHL_BaseVM;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.zkoss.bind.annotation.AfterCompose;
import org.zkoss.bind.annotation.BindingParam;
import org.zkoss.bind.annotation.Command;
import org.zkoss.bind.annotation.ContextParam;
import org.zkoss.bind.annotation.ContextType;
import org.zkoss.bind.annotation.Init;
import org.zkoss.bind.annotation.NotifyChange;
import org.zkoss.util.resource.Labels;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.event.CheckEvent;
import org.zkoss.zk.ui.select.Selectors;
import org.zkoss.zk.ui.select.annotation.Wire;
import org.zkoss.zul.Include;
import org.zkoss.zul.Window;

/**
 *
 * @author m2434
 */
public class WDITM144_VM extends WHL_BaseVM{
    protected Log logger = LogFactory.getLog(getClass());
    private List<ITM4010VO> items = new ArrayList<ITM4010VO>();
    
    private ITM4010VO cond = new ITM4010VO();//query condition;
    private ITM4010VO selected;
    private int selectedIndex;
    private List<ITM4010VO> selecteds = new ArrayList<ITM4010VO>();
    
    private String actionFlag;
    
//    private WDITM141_Service service;
    
     ///<editor-fold defaultstate="collapsed" desc="binding UI component">
    @Wire("#inc1")
    private Include inc1;
    //</editor-fold>

    public WDITM144_VM(){
        setProgTitle(Labels.getLabel("itm144.label.program_title"));
        setProgCode("WDITM144");
    }
    
//    @Init(superclass = true)
//    public void init(){
//        service = new Wditm141Service(this);
//
//        //初始化下拉選單內容
////        dayOfWeekList = service.getLoadOption().getDayOfWeek();
////        pmOffList = service.getLoadOption().getYesOrNo();
//    }
    
    @Command
    @NotifyChange("items")
    public void copy(@BindingParam("target") ITM4010VO target){
        int selectedIndex = this.getItems().indexOf(target);
        ITM4010VO copyVo = null;
        
        try{
            copyVo = (ITM4010VO) deepCopy(target);
        }catch(Exception e){
            logger.error(e.getMessage(), e);
        }
        
        this.getItems().add(selectedIndex, copyVo);
    }
    
    @Command
    public void preInsert(){
        this.setActionFlag("insert");
        
          ((Window)inc1.getFellow("operatorWin")).doModal();
    }
    
    @AfterCompose
    public void afterCompose(@ContextParam(ContextType.VIEW) Component view){
        Selectors.wireComponents(view, this, false);
    }
    
    
    
    
    //<editor-fold defaultstate="collapsed" desc="getter , setter">
    public List<ITM4010VO> getItems() {
        return items;
    }
    
    public void setItems(List<ITM4010VO> items) {
        this.items = items;
    }

    public ITM4010VO getCond() {
        return cond;
    }

    public void setCond(ITM4010VO cond) {
        this.cond = cond;
    }

    public ITM4010VO getSelected() {
        return selected;
    }

    public void setSelected(ITM4010VO selected) {
        this.selected = selected;
    }

    public List<ITM4010VO> getSelecteds() {
        return selecteds;
    }

    public void setSelecteds(List<ITM4010VO> selecteds) {
        this.selecteds = selecteds;
    }

    public int getSelectedIndex() {
        return selectedIndex;
    }

    public void setSelectedIndex(int selectedIndex) {
        this.selectedIndex = selectedIndex;
    }
    
    public String getActionFlag() {
        return actionFlag;
    }

    public void setActionFlag(String actionFlag) {
        this.actionFlag = actionFlag;
    }
    
    //</editor-fold>



    
    @Init
    public void init(){
        ITM4010VO vo1 = new ITM4010VO();
        vo1.setMONTH_WEEK("1");
        vo1.setITM4006_SEQ_NO("2015HK7019");
        vo1.setITEM_NO("09");
        vo1.setPLAN_HOUR("16");
        vo1.setPLAN_REMARK("this is remark 1");
        
        ITM4010VO vo2 = new ITM4010VO();
        vo2.setMONTH_WEEK("2");
        vo2.setITM4006_SEQ_NO("2015TPE0006");
        vo2.setITEM_NO("00");
        vo2.setPLAN_HOUR("8");
        vo2.setPLAN_REMARK("this is remark 2");
        
        items.add(vo1);
        items.add(vo2);
//        items.add(vo3);
//        items.add(vo4);
        
    }
    
    
    /**
     * make a full copy of a VO
     *
     * @param oldObj
     * @return
     * @throws Exception
     */
    protected Object deepCopy(Object oldObj) throws Exception {
        ObjectOutputStream oos = null;
        ObjectInputStream ois = null;
        try {
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            oos = new ObjectOutputStream(bos);
            oos.writeObject(oldObj);
            oos.flush();
            ByteArrayInputStream bin = new ByteArrayInputStream(bos.toByteArray());
            ois = new ObjectInputStream(bin);
            return postCopyProcess(ois.readObject());
        } catch (Exception e) {
            throw e;
        } finally {
            oos.close();
            ois.close();
        }
    }
    
    /**
     * Manipulation VO after copy process
     *
     * @param m_VO
     * @return
     */
    protected Object postCopyProcess(Object m_VO) {
        HashMap<String, Object> defprops = new HashMap<String, Object>();
        defprops.put("row_id", null);
        defprops.put("modified", false);
//        defprops.put("identifyId", IDGenerator.createID());
        defprops.put("ENTRY_USER", null);
        defprops.put("ENTRY_OFFICE", null);
        defprops.put("ENTRY_DATE", null);
        defprops.put("MODIFY_USER", null);
        defprops.put("MODIFY_OFFICE", null);
        defprops.put("MODIFY_DATE", null);
        try {
            BeanUtils.populate(m_VO, defprops);
        } catch (Exception e) {
            logger.debug(e.getMessage());
        }
        return m_VO;
    }
}
